//
//  MYCommonTool.m
//  HZTC_YaDeArt
//
//  Created by MayerF on 17/3/31.
//  Copyright © 2017年 HZTC. All rights reserved.
//

#import "MYCommonTool.h"
#import "NSString+NormalTool.h"

@implementation MYCommonTool

+ (void)toolChangeScreenOrientation:(UIInterfaceOrientation)aUIInterfaceOrientation
                 withViewController:(UIViewController *)viewController
{
    BOOL isHidden = YES;
    if (aUIInterfaceOrientation == UIDeviceOrientationPortrait) {
        isHidden = NO;
    }
    [[UIApplication sharedApplication] setStatusBarOrientation: aUIInterfaceOrientation animated:YES];
    [viewController.navigationController setNavigationBarHidden:isHidden animated:NO];//必须置于设置状态栏方向后面
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    //    self.view.transform = CGAffineTransformIdentity;
    UIInterfaceOrientation orientation = aUIInterfaceOrientation;
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    CGRect frame = [UIScreen mainScreen].bounds;
    CGPoint center = CGPointMake(frame.size.width/2, frame.size.height/2);
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        transform = CGAffineTransformMakeRotation(M_PI*1.5);
    }else if (orientation == UIInterfaceOrientationLandscapeRight) {
        transform = CGAffineTransformMakeRotation(M_PI/2);
    }else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
        transform = CGAffineTransformMakeRotation(-M_PI);
    }else {
        transform = CGAffineTransformIdentity;
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    viewController.view.transform = transform;
    viewController.view.center = center;
    [UIView commitAnimations];
    [viewController.view layoutIfNeeded];
    [viewController.view setNeedsLayout];
    
}

+ (void)commonGetIdentifyCodeTextWithTimes:(NSInteger)times
                                actionBack:(void(^)(BOOL isYes, id result))actionBack
{
    
    __block NSInteger nMax = times;
    [[HZUserManager manager] addTimerWithTime:1.f repeat:YES completed:^{
        nMax -= 1;
        if (nMax <= 0) {
            actionBack(NO, @"获取验证码");
            [[HZUserManager manager] removeTimer];
            return;
        }
        NSString *pTitle = [NSString stringWithFormat:@"%lds后重新获取",(long)nMax];
        actionBack(YES, pTitle);
    }];
}

+ (int)getHourAndMinute:(long long)totalTime
{
    char buf[8];
    char temp[64];
    memset(buf, 0, sizeof(8));
    memset(temp, 0, sizeof(64));
    sprintf(temp, "%lld", totalTime);
    memcpy(buf, temp+8, 4);
    int result = atoi(buf);
    
    return result;
}

+ (NSString *)getHourAndMinuteString:(int)time
{
    NSString *pTime = [NSString stringWithFormat:@"%4.d",time];
    NSString *pResult = [NSString stringWithFormat:@"%@:%@",[pTime substringToIndex:2],[pTime substringFromIndex:2]];
    return pResult;
}

+ (NSTimeInterval)getTimeIntervalWithLongDate:(long long)longDate
{
    return [self getTimeIntervalWithDate:[NSString stringWithFormat:@"%lld",longDate]];
}

+ (NSTimeInterval)getTimeIntervalWithDate:(NSString *)dateStr
{
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    //    [formater setLocale:[NSLocale currentLocale]];
    [formater setDateFormat:@"yyyyMMddHHmmss"];
    NSDate *newDate = [formater dateFromString:dateStr];
    return [newDate timeIntervalSince1970];
}

+ (NSString *)shareToolGetTimeStrWithTimeInterval:(int)timeInterval
{
    //时间戳转时间
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    //    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    NSString *timeStrT = [formatter stringFromDate:time];
    return timeStrT;
}

+ (long long)shareToolGetLongTimeWithTimeInterval:(int)timeInterval
{
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *timeStrT = [formatter stringFromDate:time];
    if (!timeStrT) {
        return 0;
    }else{
        return [timeStrT longLongValue];
    }
}

+ (NSString *)shareToolGetCurrentTime
{
    NSDate *pNowTime = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *pTime = [formatter stringFromDate:pNowTime];
    return pTime;
}

+ (NSString *)commonToolGetYMDTime
{
    NSDate *pNowTime = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *pTime = [formatter stringFromDate:pNowTime];
    return pTime;
}

+ (NSString *)commonToolYMDTimeStrWithTimeInterval:(int)timeInterval
{
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd"];
    NSString *timeStrT = [formatter stringFromDate:time];
    return timeStrT;
}

+ (void)commonToolYMDTimeWithStart:(NSString **)start
                               end:(NSString **)end
                            endDay:(int)endDay
                          rangeDay:(int)rangeDay
{
    double dCurrentTime = [[NSDate date] timeIntervalSince1970];//当天日期
    double dEndTime = dCurrentTime - endDay*24*60*60;//结束时间
    double dStartTime = dEndTime - (rangeDay-1)*24*60*60;//开始时间
    *end = [self commonToolYMDTimeStrWithTimeInterval:dEndTime];
    *start = [self commonToolYMDTimeStrWithTimeInterval:dStartTime];
}

+ (NSDate *)commonToolDateWithTimeStr:(NSString *)timeStr
{
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    [formater setLocale:[NSLocale currentLocale]];
    [formater setDateFormat:@"yyyy-MM-dd"];
    NSDate *newDate = [formater dateFromString:timeStr];
    return newDate;
}

+ (NSString *)commonToolDateStrWithDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString  *string = [[NSString alloc]init];
    string = [dateFormatter stringFromDate:date];
    return string;
}

//十六进制转换为十进制表示颜色
/*
 *_rgb2uic(i,a) [UIColor colorWithRed:(i>>16&0xFF)/255.0 green:(i>>8&0xFF)/255.0 blue:(i&0xFF)/255.0 alpha:a] int值究竟是 ＝ B + (G * 256) + (R * 65536)还是 ＝ R + (G * 256) + (G * 65536)；由(i>>16&0xFF)这些决定
 */
+ (int)shareToolTenColorWithColorStr:(NSString *)colorStr andDecimal:(int)decimal
{
    //颜色的字符串长度为6
    if (colorStr.length > 6) {
        return 0;//黑色hx
    }
    int result, R, G, B;
    
    R = [[colorStr substringWithRange:NSMakeRange(0, 1)] getTenDecimalNum] * decimal + [[colorStr substringWithRange:NSMakeRange(1, 1)] getTenDecimalNum];
    G = [[colorStr substringWithRange:NSMakeRange(2, 1)] getTenDecimalNum] * decimal + [[colorStr substringWithRange:NSMakeRange(3, 1)] getTenDecimalNum];
    B = [[colorStr substringWithRange:NSMakeRange(4, 1)] getTenDecimalNum] * decimal + [[colorStr substringWithRange:NSMakeRange(5, 1)] getTenDecimalNum];
    result = B + (G * 256) + (R * 65536);
    
    return result;
}

+ (NSString *)getFilePathWithFileName:(NSString *)name
{
    NSString *pDocPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *pFilePath = [pDocPath stringByAppendingPathComponent:name];
    return pFilePath;
}

+ (void)shareToolWriteToFile:(NSString *)fileName content:(id)content
{
    NSString *pFullPath = [self getFilePathWithFileName:fileName];
    if ([content isKindOfClass:[NSDictionary class]] ||
        [content isKindOfClass:[NSArray class]] ||
        [content isKindOfClass:[NSData class]] ||
        [content isKindOfClass:[NSString class]]) {
        
        BOOL isSuccess = [content writeToFile:pFullPath atomically:YES];
        DLog(@"isSuccess==%d",isSuccess);
    }
}

+ (id)shareToolReadFileName:(NSString *)fileName readFileType:(KReadFileType)readFileType
{
    id result = nil;
    NSString *pFullPath = [self getFilePathWithFileName:fileName];
    switch (readFileType) {
            case KReadFileDictionaryType:
        {
            result = [NSDictionary dictionaryWithContentsOfFile:pFullPath];
        }
            break;
            case KReadFileArrayType:
        {
            result = [NSArray arrayWithContentsOfFile:pFullPath];
        }
            break;
            case KReadFileDataType:
        {
            result = [NSData dataWithContentsOfFile:pFullPath];
        }
            break;
            
        default:
            break;
    }
    
    return result;
}


@end
